package com.sprucetec.live.controller;

import com.google.common.collect.Lists;
import com.sprucetec.commons.util.CollectionUtils;
import com.sprucetec.live.base.entity.TreeNode;
import com.sprucetec.live.common.util.ServiceResult;
import com.sprucetec.live.common.util.ServiceResultUtil;
import com.sprucetec.live.util.SsoUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Set;

/**
 * @Description: 树形菜单
 * @author kenny
 */
@RestController
@RequestMapping(value = "/menu")
public class MenuController extends BaseController{


	@RequestMapping(value="/getAllMenu", method = {RequestMethod.GET})
	@ResponseBody
	public ServiceResult<List<TreeNode>> getAllMenu() {

		List<TreeNode> treeNodeList = Lists.newArrayList();

		TreeNode mainTreeNode = new TreeNode();
		mainTreeNode.setText("首页");
		mainTreeNode.setHref("/");
		mainTreeNode.setTarget("");
		mainTreeNode.setIsFilter("0");
		mainTreeNode.setSubMenu(null);
		treeNodeList.add(mainTreeNode);

		// 直播管理
		TreeNode liveVideoNode = new TreeNode();
		liveVideoNode.setText("直播管理");
		liveVideoNode.setHref("/liveVideoList");
		liveVideoNode.setTarget("");
		liveVideoNode.setIsFilter("1");
		liveVideoNode.setSubMenu(null);
		treeNodeList.add(liveVideoNode);

		// 聊天管理
		TreeNode liveChatNode = new TreeNode();
		liveChatNode.setText("聊天管理");
		liveChatNode.setHref("/liveChatList");
		liveChatNode.setTarget("");
		liveChatNode.setIsFilter("1");
		liveChatNode.setSubMenu(null);
		treeNodeList.add(liveChatNode);

		// 基础管理
		TreeNode liveBasicParent = new TreeNode();
		liveBasicParent.setText("基础管理");
		liveBasicParent.setHref("/liveBasic");
		liveBasicParent.setTarget("");
		liveBasicParent.setIsFilter("1");
		liveBasicParent.setSubMenu(null);

		// 主播管理
		TreeNode liveAnchorChild = new TreeNode();
		liveAnchorChild.setText("主播管理");
		liveAnchorChild.setHref("/liveAnchorList");
		liveAnchorChild.setTarget("");
		liveAnchorChild.setIsFilter("1");
		liveAnchorChild.setSubMenu(null);

		// 用户管理
		TreeNode liveConsumerChild = new TreeNode();
		liveConsumerChild.setText("用户管理");
		liveConsumerChild.setHref("/liveUserList");
		liveConsumerChild.setTarget("");
		liveConsumerChild.setIsFilter("1");
		liveConsumerChild.setSubMenu(null);

		// 分类管理
		TreeNode liveCategoryChild = new TreeNode();
		liveCategoryChild.setText("分类管理");
		liveCategoryChild.setHref("/liveCategoryList");
		liveCategoryChild.setTarget("");
		liveCategoryChild.setIsFilter("1");
		liveCategoryChild.setSubMenu(null);

		// 内网IP
		TreeNode liveIntranetIpList = new TreeNode();
		liveIntranetIpList.setText("内网IP");
		liveIntranetIpList.setHref("/liveIntranetIpList");
		liveIntranetIpList.setTarget("");
		liveIntranetIpList.setIsFilter("1");
		liveIntranetIpList.setSubMenu(null);


		List<TreeNode> techMenuList = Lists.newArrayList();
		techMenuList.add(liveAnchorChild);
		techMenuList.add(liveConsumerChild);
		techMenuList.add(liveCategoryChild);
		techMenuList.add(liveIntranetIpList);
		liveBasicParent.setSubMenu(techMenuList);
		treeNodeList.add(liveBasicParent);

		//系统管理
		TreeNode systemParent = new TreeNode();
		systemParent.setText("系统管理");
		systemParent.setHref("/liveSystemManage");
		systemParent.setTarget("");
		systemParent.setIsFilter("1");
		systemParent.setSubMenu(null);
		// 系统日志
		TreeNode loginNode = new TreeNode();
		loginNode.setText("系统日志");
		loginNode.setHref("/liveSystemLog");
		loginNode.setTarget("");
		loginNode.setIsFilter("1");
		loginNode.setSubMenu(null);
		// 缓存管理
		TreeNode cache = new TreeNode();
		cache.setText("缓存管理");
		cache.setHref("/liveSystemCache");
		cache.setTarget("");
		cache.setIsFilter("1");
		cache.setSubMenu(null);

		// 构建系统树
		List<TreeNode> systemList = Lists.newArrayList();
		systemList.add(loginNode);
		systemList.add(cache);
		systemParent.setSubMenu(systemList);
		treeNodeList.add(systemParent);

		// 合法的权限列表
		Set<String> authUrls = SsoUtil.getAccessUrls();
		// auth为空只显示不走权限菜单
		if (authUrls == null) {
			this.removeAuth(treeNodeList);
		}
		// auth 不为空判断是否有权限数据
		if (authUrls != null) {
			this.removeAuthData(treeNodeList, authUrls);
		}
		return ServiceResultUtil.success(true, 1, "获取菜单成功", treeNodeList);
	}

	private void removeAuth(List<TreeNode> treeNodeList) {
		// 移除有权限的菜单
		treeNodeList.removeIf(treeNode -> "1".equals(treeNode.getIsFilter()));
		// 处理子集
		treeNodeList.forEach(this::removeChild);
	}

	private void removeChild(TreeNode treeNode) {
		List<TreeNode> subMenuList = treeNode.getSubMenu();
		if (CollectionUtils.isNotEmpty(subMenuList)) {
			this.removeAuth(subMenuList);
		}
	}

	private void removeAuthData(List<TreeNode> treeNodeList, Set<String> authUrls) {
		//移除没有权限数据的 菜单
		treeNodeList.removeIf(treeNode -> "1".equals(treeNode.getIsFilter()) && !authUrls.contains(treeNode.getHref()));
		//处理子集
		treeNodeList.forEach(treeNode -> this.removeChildData(treeNode, authUrls));
	}

	private void removeChildData(TreeNode treeNode, Set<String> authUrls) {
		List<TreeNode> subMenuList = treeNode.getSubMenu();
		if(!CollectionUtils.isEmpty(subMenuList)){
			removeAuthData(subMenuList, authUrls);
		}
	}

}
